import { RouteRecordRaw, RouterView } from 'vue-router';
import { h, markRaw } from 'vue';
import Default from '/src/layout/index.vue';



// 根路由
export const ROOT_ROUTE: RouteRecordRaw = {
    path: '/',
    name: 'root',
    meta: { title: '首页' },
    component: markRaw(Default),
    redirect: '',
    children: [],
}

// 登录页面
export const LOGIN_ROUTE: RouteRecordRaw = {
    path: '/login',
    name: 'login',
    meta: { title: '登录' },
    component: () => import('@/pages/Login.vue'),
}

// 404页面
export const NOTFOUND_ROUTE: RouteRecordRaw = {
    path: '/404',
    name: '404',
    meta: { title: 'NOT FOUND' },
    component: () => import('@/pages/404.vue'),
}

const modules = import.meta.glob('@/pages/**/*.vue');

interface Route {
    menu_name: string,
    router_path: string,
    page_path: string
    menu_type: number,
    menu_key: string,
    icon: string,
    children?: [],
    is_show: boolean,
}

/**
 * 格式化路由
 * @param routes
 * @param parentRoute
 */
export function formatRouters(routes: Route[], parentRoute: RouteRecordRaw | null = null) {
    return routes.map((route) => {
        const routeRecord = createRoute(route, parentRoute)
        if (route.children != null && route.children && route.children.length) {
            routeRecord.children = formatRouters(route.children, routeRecord)
        }
        return routeRecord
    })
}

/**
 * 创建路由
 * @param route
 * @param parentRoute
 */
const createRoute = function (route: Route, parentRoute: RouteRecordRaw | null = null): RouteRecordRaw {
    const record: RouteRecordRaw = {
        path: `${route.router_path}`,
        name: route.menu_key,
        component: markRaw(RouterView),
        meta: {
            title: route.menu_name,
            icon: route.icon,
            type: route.menu_type,
            show: route.is_show,
            view: route.page_path,
            parent_route: parentRoute ? parentRoute.meta : parentRoute
        }
    }

    if (route.menu_type == 0) {
        record.component = markRaw(RouterView);
        if (route.children != null && route.children && route.children.length) {
            record.redirect = findFirstValidRoute(route.children);
        }
    } else {
        record.component = modules[`/src${route.page_path}.vue`]
    }

    return record
}

/**
 * 获取首条有效路由
 * @param routes
 * @returns
 */
export function findFirstValidRoute(routes: any[]): string | undefined {
    for (const route of routes) {
        if (route.menu_type == 1 && route.is_show) {
            return route.router_path as string
        }
        if (route.children) {
            const router_path = findFirstValidRoute(route.children)
            if (router_path) {
                return router_path
            }
        }
    }
}


export function formatMenu(routes: any[], isChild: boolean = false) {
    return routes.map((route) => {
        const vnode = h('span', { class: 'iconfont !text-[16px] icon-' + route.meta?.icon })
        const menuItem: { key: string; label: string; title: string; icon: any; children?: any[] } = {
            key: route.path,
            label: route.meta.title,
            title: route.meta.title,
            icon: vnode,
        }
        if (isChild) {
            if (route.children != null && route.children && route.children.length) {
                menuItem.children = formatMenu(route.children)
            }
        }

        return menuItem;
    })
}